package design;
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.jdom.Attribute;

/**
 * Changes how the tree displays elements.
 */
public class XMLTreeCellRenderer extends DefaultTreeCellRenderer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// colors for tree items
	private final Color elementColor = new Color(0, 0, 0);
	private final Color textColor = Color.BLUE;
	//private final Color attrColor = Color.DARK_GRAY;

	// remove icons
	public XMLTreeCellRenderer() {
		setOpenIcon(new ImageIcon("images/open.gif"));
		setClosedIcon(new ImageIcon("images/close.gif"));
		setLeafIcon(new ImageIcon("images/leaf.gif"));
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		JDOMAdapterNode adapterNode = (JDOMAdapterNode) value;
	
		if (adapterNode.node.getChildren().size() > 0) {
			if (adapterNode.node.getAttributes().size() == 0) 
				value = adapterNode.node.getName();
		}
		else {
			if (adapterNode.node.getAttributes().size() == 0 && adapterNode.node.getTextTrim().length() != 0) 
				value = adapterNode.node.getName() + " : " + adapterNode.node.getTextTrim() ;			
		}
		
		if (adapterNode.node.getAttributes().size() > 0 ) {
			if(adapterNode.node.getTextTrim().length()!= 0) value = adapterNode.node.getName() + " : " + adapterNode.node.getTextTrim() ;
			else{
				String str = adapterNode.node.getName();
				int no = 0;
				for (int i = 0; i < adapterNode.node.getAttributes().size(); i++) {
					if (((Attribute) adapterNode.node.getAttributes().get(i)).getName().contains("Id")) {
						if(no == 0)	str += " [";
						else  str += " ; ";
						String[] ele = null;
						if (((Attribute) adapterNode.node.getAttributes().get(i)).getValue().contains(":")) {
							ele = ((Attribute) adapterNode.node.getAttributes().get(i)).getValue().split(":");
							str += ((Attribute) adapterNode.node.getAttributes().get(i)).getName()
									+ " = " + ele[ele.length - 1] ;
						} else
							str += ((Attribute) adapterNode.node.getAttributes().get(i)).getName()
						     		+ "= " + ((Attribute) adapterNode.node.getAttributes().get(i)).getValue() ;
						no = no + 1;
					}
					else if (((Attribute) adapterNode.node.getAttributes().get(i)).getName().contains("Effect")) {
						if(no == 0)	str += " [";
						else 	str += " ; ";
						str += ((Attribute) adapterNode.node.getAttributes().get(i)).getName()
								+ "= " + ((Attribute) adapterNode.node.getAttributes().get(i)).getValue();
						no ++;
					}
					else if (((Attribute) adapterNode.node.getAttributes().get(i)).getName().contains("DataType")) {
						if(no == 0)	str += " [";
						else	str += " ; ";
						String[] ele = null;
						if (((Attribute) adapterNode.node.getAttributes().get(i)).getValue().contains("urn")) {
							ele = ((Attribute) adapterNode.node.getAttributes().get(i)).getValue().split(":");
							str +=((Attribute) adapterNode.node.getAttributes().get(i)).getName()
									+ " = " + ele[ele.length - 1] ;
						} else
							str +=((Attribute) adapterNode.node.getAttributes().get(i)).getName()
								   + "= " + ((Attribute) adapterNode.node.getAttributes().get(i)).getValue();
						no ++;
					} else if (((Attribute) adapterNode.node.getAttributes().get(i)).getName().contains("ContextPath")) {
						if(no == 0)	str += " [";
						else	str += " ; ";
						str += ((Attribute) adapterNode.node.getAttributes().get(i)).getName()
								+ "= " + ((Attribute) adapterNode.node.getAttributes().get(i)).getValue() ;
						no ++;
					}					
					if(i == adapterNode.node.getAttributes().size() - 1&& str.contains(" ["))
						str += " ]";
					if(i == adapterNode.node.getAttributes().size())no = 0;
				}					
				value = str;
			}
		}
		
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

		if (!selected) {
			if (adapterNode.node.getTextTrim().length() == 0) {
				setForeground(elementColor);
			} else {
				setForeground(textColor);
			}
		}
		return this;
	}
}
